const logoinIdValidator = new FieldValidator("txtLoginId", function (val) {
  if (!val) {
    return "请填写账号";
  }
});

const loginPwdValidator = new FieldValidator("txtLoginPwd", function (val) {
  if (!val) {
    return "请填写密码";
  }
});

const form = $(".user-form");
form.onsubmit = async function (e) {
  e.preventDefault();
  const ret = await FieldValidator.validate(
    logoinIdValidator,
    loginPwdValidator
  );

  if (!ret) {
    return;
  }

  const formData = new FormData(form);
  const resp = await API.login(Object.fromEntries(formData.entries()));
  if (resp.code === 0) {
    alert("登陆成功");
    location.href = "./index.html";
  } else {
    loginPwdValidator.p.innerText = "登陆失败，请检查账号或密码";
    loginPwdValidator.input.value = "";
  }
};
